//
//  HQAPPConfigInfo.m
//  HqAppArchitect
//
//  Created by litianqi on 2021/11/30.
//  Copyright © 2021 edu24ol. All rights reserved.
//

#import "HQBaseConfig.h"
#import "HQUserIdentifier.h"

@implementation HQBaseConfig
+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    static HQBaseConfig *obj = nil;
    dispatch_once(&onceToken, ^{
        obj = [[HQBaseConfig alloc] init];
        
    });
    return obj;
}



- (NSString *)token
{
    if (self.getTokenBlock) {
        return self.getTokenBlock();
    }
    return @"";
}

- (long long)userId
{
    if (self.getUserIdBlock) {
        return self.getUserIdBlock();
    }
    return 0;
}

- (NSString *)userName
{
    if (self.getUserNameBlock) {
        return  self.getUserNameBlock();
    }
    return @"";
}

- (NSString *)nickName
{
    if (self.getNickNameBlock) {
        return  self.getNickNameBlock();
    }
    return @"";
}

- (NSString *)faceUrl
{
    if (self.getFaceUrlBlock) {
        return self.getFaceUrlBlock();
    }
    return @"";
}

- (NSString *)unitsPrefixText
{
    if (self.getUnitsPrefixTextBlock) {
        return self.getUnitsPrefixTextBlock();
    }
    return _unitsPrefixText.length ? _unitsPrefixText :@"";
}

- (NSString *)intendIdList
{
    if (self.getIntendIdList) {
        return self.getIntendIdList();
    }
    return _intendIdList.length ? _intendIdList :@"";
}


- (NSInteger)intendId
{
    if (self.getIntendId) {
        return self.getIntendId();
    }
    return _intendId;
}

- (BOOL)isNormalUser
{
    return [HQUserIdentifier isNormalUser];
}


- (NSDictionary<NSString *,NSString *> *)specialReqUrlDominDic
{
    if (!_specialReqUrlDominDic) {
        _specialReqUrlDominDic = @{};
    }
    
    return  _specialReqUrlDominDic;
}

- (void)addCustomUrlPath:(NSString *)urlPath toDomain:(NSString *)domain
{
    if (urlPath.length == 0 || domain.length == 0) {
        NSLog(@"url | domain is nil");
        return;
    }
    
    NSMutableDictionary *dic = self.specialReqUrlDominDic.mutableCopy;
    [dic setObject:domain forKey:urlPath];//update
    self.specialReqUrlDominDic = dic.copy;
}

+ (UIImage *)themeFillImage:(UIImage *)image
{
    if (!image) {
        return image;
    }
    
    if (HQBaseConfig.shareInstance.themeColor) {
        UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0, image.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetBlendMode(context, kCGBlendModeNormal);
        CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
        CGContextClipToMask(context, rect, image.CGImage);
        [HQBaseConfig.shareInstance.themeColor setFill];
        CGContextFillRect(context, rect);
        UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return colorImage;
    };
    
    return image;
}

@end

@implementation HQBaseConfig (UserRole)
+ (void)hq_checkUserRoleResult:(void(^)(BOOL isNormal))resultBlock
{
    if (HQBaseConfig.shareInstance.isNormalUser) {
        HQBaseConfig.shareInstance.unitsPrefixText = [HQUserIdentifier unitsPrefixText];
        if (resultBlock) {
            resultBlock(YES);
        }
        return;
    }
    
    [HQUserIdentifier updateUserIdenfityInfoWithVersionId:HQBaseConfig.shareInstance.buildId appid:HQBaseConfig.shareInstance.appId deviceId:HQBaseConfig.shareInstance.deviceId account:HQBaseConfig.shareInstance.userName uid:[@(HQBaseConfig.shareInstance.userId) stringValue] result:^(HqUserType type) {
        
//        HQBaseConfig.shareInstance.isNormalUser = (type == 0 ? YES :NO);
        HQBaseConfig.shareInstance.unitsPrefixText = [HQUserIdentifier unitsPrefixText];
        if (resultBlock) {
            resultBlock(type == 0);
        }
    }];
}
@end

